package cn.wolfcode.wolf2w.listener;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.util.List;

//@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private IStrategyStatisVORedisService strategyStatisVORedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        System.out.println("初始化开始");
        //vo缓存的初始化操作
//1、获取所有攻略对象
        List<Strategy> list = strategyService.list();
        for (Strategy strategy : list) {
            //如果发现redis中已经存在某个vo对象,直接跳过,不是初始化.
            if (strategyStatisVORedisService.isVOExists(strategy.getId())){
                         continue;
            }
            //初始化
            StrategyStatisVO vo = new StrategyStatisVO();
            BeanUtils.copyProperties(strategy, vo);
            //重新保存
            strategyStatisVORedisService.setStrategyStatisVO(vo);

        }
        System.out.println("初始化完成");
    }
}
